RenderType = Enum{
    "Mesh",
    "Text",
    "Scissor",
    "ScissorOff"
}
local gets = {}
local sets = {}
Renderer = {
    name = "renderer",
    props = {
        renderInfoDirty = true,
        renderable = true
    },
    sets = sets,
    gets = gets
}
local function onSizeChanged(self)
    self.renderInfoDirty = true
    self._assembler:setVertexDirty()
end
local function onTransformDirty(self)
    self.renderInfoDirty = true
    self._assembler:setVertexDirty()
end
function Renderer:ctor()
    self._assembler = Assembler.new(self)
    self:on(NodeEvent.TransformDirty, onTransformDirty, self)
    self:on(NodeEvent.WidthChanged, onSizeChanged, self)
    self:on(NodeEvent.HeightChanged, onSizeChanged, self)
end

function gets:color(key)
    return self._color
end

function Renderer:rgba()
    if self._color then
        return self._color:unpack()
    end
    return 1, 1, 1, 1
end

function sets:color(key, val)
    self._color = val
    self:setColorDirty()
end

function gets:material(key)
    if not self._material then
        self._material = self:createDefaultMaterial()
        self.renderInfoDirty = true
    end
    return self._material
end

function sets:material(key, val)
    self._material = val
    self.renderInfoDirty = true
end

function Renderer:createDefaultMaterial()
    return Material.default
end

function Renderer:getRenderInfo()
    if self.renderInfoDirty then
        self._renderInfo = {}
        self._renderInfo.renderType = RenderType.Mesh
        self._renderInfo.vertices = self._assembler:getVertices()
        self._renderInfo.vertexMap = self._assembler:getVertexMap()
        self._renderInfo.material = self.material
        self._renderInfo.cmp = self
        self.renderInfoDirty = false
    end

    return self._renderInfo
end

function Renderer:setColorDirty()
    self.renderInfoDirty = true
    self._assembler:setColorDirty()
end
